package com.community.common.controller;


import com.community.common.pojo.PageRequest;
import com.community.common.pojo.domain.Building;
import com.community.common.pojo.domain.Community;
import com.community.common.pojo.domain.House;
import com.community.common.pojo.domain.OwnerHouse;
import com.community.common.pojo.dto.CommunityCondition;
import com.community.common.pojo.response.LayuiPage;
import com.community.common.pojo.response.MyResponse;
import com.community.common.service.CommunityService;
import com.community.common.service.OwnerHouseService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 小区表 前端控制器
 * </p>
 *
 * @author superman
 * @since 2020-08-02
 */
@Controller
@RequestMapping("/community")
public class CommunityController {

    @Resource
    private CommunityService communityService;

    @Resource
    OwnerHouseService ownerHouseService;

    @ResponseBody
    @PostMapping("/add")
    public MyResponse add(Community community) {
        return communityService.add(community);
    }

    @ResponseBody
    @PostMapping("/delete")
    public MyResponse delete(@RequestBody List<Long> ids) {
        boolean delete = communityService.delete(ids);
        if (delete) {
            return MyResponse.success();
        }
        return MyResponse.fail();
    }

    @ResponseBody
    @PostMapping("/edit")
    public MyResponse edit(Community community) {
        boolean edit = communityService.edit(community);
        if (edit) {
            return MyResponse.success();
        }
        return MyResponse.fail();
    }

    @GetMapping("editPage")
    public String editPage(Long id, ModelMap map) {
        Community community = communityService.getById(id);
        map.put("community", community);
        return "manage/community_edit";
    }

    @ResponseBody
    @PostMapping("/queryByPage")
    public LayuiPage queryByPage(@RequestBody PageRequest<CommunityCondition> communityConditionPageRequest) {
        List<Community> communityList = communityService.queryByPage(communityConditionPageRequest);
        return LayuiPage.parsePage(communityList);
    }

    /**
     * 查询所有小区
     *
     */
    @ResponseBody
    @GetMapping("/queryAllCommunity")
    public MyResponse queryAllCommunity(@RequestParam(required = false) String openId) {
        List<Community> communities = communityService.queryAllCommunity(openId);
        return MyResponse.success(communities);
    }

    /**
     * 查询小区下面所有楼栋
     */
    @ResponseBody
    @GetMapping("/queryAllBuilding/{xqId}")
    public MyResponse queryAllBuilding(@PathVariable String xqId) {
        List<Building> communities = communityService.queryAllBuilding(xqId);
        return MyResponse.success(communities);
    }

    /**
     * 查询楼栋下面所有房屋
     */
    @ResponseBody
    @GetMapping("/queryAllHouse/{ldId}")
    public MyResponse queryAllHouse(@PathVariable String ldId) {
        List<House> communities = communityService.queryAllHouse(ldId);
        return MyResponse.success(communities);
    }

    /*-----------------------------------------用户对小区的操作 添加小区 楼栋 房屋 提交审核--------------------------------------------*/

    /**
     * 用户添加小区
     */
    @ResponseBody
    @PostMapping("/user/community")
    public MyResponse userCommunityAdd(@RequestBody OwnerHouse ownerHouse) {
        ownerHouseService.add(ownerHouse);
        return MyResponse.success();
    }

    /**
     * 用户修改小区下面房屋
     */
    @ResponseBody
    @PutMapping("/user/community")
    public MyResponse userCommunityUpdate(@RequestBody OwnerHouse ownerHouse) {
        ownerHouseService.update(ownerHouse);
        return MyResponse.success();
    }

    /**
     * 用户删除小区
     */
    @ResponseBody
    @DeleteMapping("/user/community")
    public MyResponse userCommunityDel(@RequestBody OwnerHouse ownerHouse) {
        ownerHouseService.delete(ownerHouse);
        return MyResponse.success();
    }

    /**
     * 用户查询小区列表
     * <p>
     * TODO 页面上面需要展示出当前小区审核状态 待审核 已通过 不通过
     */
    @ResponseBody
    @GetMapping("/user/community/{openId}")
    public MyResponse userCommunityList(@PathVariable String openId) {
        return MyResponse.success(ownerHouseService.userCommunityList(openId));
    }

    /**
     * 用户获取小区详情
     * <p>
     * TODO 页面上面需要展示出当前小区审核状态 待审核 已通过 不通过
     */
    @ResponseBody
    @GetMapping("/user/communityDetail/{id}")
    public MyResponse userCommunityDetail(@PathVariable String id) {
        return MyResponse.success(ownerHouseService.userCommunityDetail(id));
    }

    /**
     * 将当前小区信息重新提交审核
     * 当审核不通过的时候需要重新审核
     * 初次录入待审核 - 审核通过 的状态 不需要调用此接口
     */
    @ResponseBody
    @GetMapping("/user/submitCheck/{ownerHouseId}")
    public MyResponse submitCheck(@PathVariable String ownerHouseId) {
        ownerHouseService.submitCheck(ownerHouseId);
        return MyResponse.success();
    }

}
